package myweb;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
 
/**
 * Servlet implementation class ServletDownload
 */

public class DOWNLOADServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DOWNLOADServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//获得请求文件名
		String filename = request.getParameter("filename");
		System.out.println("download"+filename);
		
		//设置文件MIME类型
		response.setContentType(getServletContext().getMimeType(filename));
		//设置Content-Disposition
		response.setHeader("Content-Disposition", "attachment;filename="+filename);
		//读取目标文件，通过response将目标文件写到客户端
		//获取目标文件的绝对路径
		String fullFileName = getServletContext().getRealPath("./WEB-INF/download" + filename);
		
		System.out.println(fullFileName);
		//读取文件
		byte[]buff=new byte[1024*10];//可以自己 指定缓冲区的大小
		InputStream in = new FileInputStream(fullFileName);
		OutputStream out = response.getOutputStream();
		
		//写文件
		int b;
		while((b=in.read(buff))!= -1)
		{
			out.write(buff,0,b);
			System.out.println(b);
		}
		
		in.close();
		out.close();
	}
 
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 验证请求是否满足要求（post 请求 / enctype 是否以multipart打头
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
       
        // 如果不满足要求就立即结束对该请求的处理
        if (!isMultipart) {
            return;
        }

        try {
        	String Filename = "";
        	String savepath = "/root/apache-tomcat-8.5.51/webapps/down&upload/";
            // FileItem 是表单中的每一个元素的封装
            // 创建一个 FileItem 的工厂类
            FileItemFactory factory = new DiskFileItemFactory();
            // 创建一个文件上传处理器（装饰设计模式）
            ServletFileUpload upload = new ServletFileUpload(factory);
            // 解析请求
            List<FileItem> items = upload.parseRequest(request);
            for (FileItem fileItem :
                    items) {
                // 判断空间是否是普通控件
                if (fileItem.isFormField()) {
                    // 普通控件
                } else {
                    // 上传控件
                	//获得文件名称
                	Filename = fileItem.getName();
                    // 将上传的文件保存到服务器
                    fileItem.write(new File(savepath,Filename));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
		
		
	
 
}
